package com.stylefeng.guns.modular.netcar.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.DispatchPsnOrgMapper;
import com.stylefeng.guns.modular.netcar.model.DispatchPsnOrg;
import com.stylefeng.guns.modular.netcar.service.IDispatchPsnOrgService;
import org.beetl.ext.fn.Json;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 调度员可调度车企 Service 实现 
 * @author sasazaixian
 * @Date 2018-11-15 10:28
 */
@Service
public class DispatchPsnOrgServiceImpl extends ServiceImpl<DispatchPsnOrgMapper,DispatchPsnOrg> implements IDispatchPsnOrgService {

    @Resource
    private DispatchPsnOrgMapper dispatchPsnOrgMapper;

    /**
     * 删除调度员可调度车企
     * @param dispatchPsnOrg
     * @return     
     */
    @Transactional
    public int deleteDispatchPsnOrg(DispatchPsnOrg dispatchPsnOrg,String token)throws Exception{
        if(dispatchPsnOrg==null ){
            throw new RuntimeException("参数不全！");
        }
        return this.dispatchPsnOrgMapper.deleteDispatchPsnOrg(dispatchPsnOrg);
    }
    /**
     * 批量删除调度员可调度车企
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchDispatchPsnOrg(String[] ids,String token)throws Exception{
        return this.dispatchPsnOrgMapper.deleteBatchDispatchPsnOrg(ids);
    }
    /**
     * 查询调度员可调度车企列表
     * @param dispatchPsnOrg 调度员可调度车企对象
     * @return
     */
    public List<DispatchPsnOrg> selectListDispatchPsnOrg(DispatchPsnOrg dispatchPsnOrg,String token)throws Exception{
        return  this.dispatchPsnOrgMapper.selectListDispatchPsnOrg(dispatchPsnOrg);
    }
    /**
	 * 分页查询调度员可调度车企列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListDispatchPsnOrg(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(dispatchPsnOrgMapper.selectListDispatchPsnOrg(page,(DispatchPsnOrg)pager.getObj()));
        List<DispatchPsnOrg> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }

    /**
     * 同一个事务 互斥锁查询
     * @param dispatchPsnOrg 调度员可调度车企对象
     * @return 
     */
    public  List<DispatchPsnOrg> selectListDispatchPsnOrgForUpdate(DispatchPsnOrg dispatchPsnOrg)throws Exception{
        return  this.dispatchPsnOrgMapper.selectListDispatchPsnOrgForUpdate(dispatchPsnOrg);
    }
    /**
     * 通过主键id查询的调度员可调度车企列表
     * @param id 主键id
     * @return     
     */
    public  DispatchPsnOrg findDispatchPsnOrgById(String id,String token)throws Exception{
        return  this.dispatchPsnOrgMapper.findDispatchPsnOrgById(id);
    }

    /**
     * 新增调度员可调度车企数据
     * @param dispatchPsnOrg  调度员可调度车企对象
     * @return
     */
    @Transactional
    public int insertDispatchPsnOrg(DispatchPsnOrg dispatchPsnOrg,String token) throws Exception {

        if(dispatchPsnOrg.getSoIdArry()==null || dispatchPsnOrg.getSoIdArry().trim().length()<1){
            throw new RuntimeException("车企信息异常!");
        }
        if(dispatchPsnOrg.getSuId()==null && (dispatchPsnOrg.getSuIdArry()==null || dispatchPsnOrg.getSuIdArry().trim().length()<1)){
            throw new RuntimeException("调度员不能为空！");
        }
        String[] soIdArry=dispatchPsnOrg.getSoIdArry().trim().split(",");
        String [] suIdArry=null;
        if(dispatchPsnOrg.getSuId()==null){
            suIdArry= dispatchPsnOrg.getSuIdArry().split(",");
        }else{
            suIdArry=new String []{dispatchPsnOrg.getSuId().toString()};
        }
        for(String suid:suIdArry) {
            dispatchPsnOrg.setSuId(Integer.parseInt(suid));
            for (String soId : soIdArry) {
                if (soId != null && soId.trim().length() > 0) {
                    dispatchPsnOrg.setDpoId(IdWorker.get32UUID());
                    dispatchPsnOrg.setSoId(Integer.parseInt(soId.trim()));
                    this.dispatchPsnOrgMapper.insertDispatchPsnOrg(dispatchPsnOrg);
                }
            }
        }
        return 0;
    }
    
    /**
     * 更新调度员可调度车企数据
     * @param dispatchPsnOrg 调度员可调度车企对象
     * @return     
     */
    @Transactional
    public int updateDispatchPsnOrg(DispatchPsnOrg dispatchPsnOrg,String token) throws Exception {     	
        return  this.dispatchPsnOrgMapper.updateDispatchPsnOrg(dispatchPsnOrg);
    }    
    /**
     * 查询调度员可调度车企返回key-Value属性
     * @param dispatchPsnOrg 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsDispatchPsnOrg(DispatchPsnOrg dispatchPsnOrg,String token) throws Exception {
        List<DispatchPsnOrg> listDispatchPsnOrg=this.dispatchPsnOrgMapper.selectListDispatchPsnOrg(dispatchPsnOrg);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(DispatchPsnOrg item:listDispatchPsnOrg){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getDpoId());
            opts.setValue(item.getDpoId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
